var msgTypesObj;
$(function(){
	initMsgTypeOpt();// 消息类型选择
	initDateTime();// 时间控件初始化为今天
});
function initDateTime(){
	var date = new Date();
	var year = date.getFullYear(); // 当前年份
	var month = date.getMonth() + 1;// 当前月份
	month = month < 10 ? "0" + month : month;
	var day = date.getDate(); // 今天
	day = day < 10 ? "0" + day : day;
	$("#startDate").val(year +"-"+month +"-"+day);
    $("#endDate").val(year +"-"+month +"-"+day);
	$('#startDate').datetimepicker({
		minView : 2,
		autoclose : true,
		format : 'yyyy-mm-dd'
	});
	
	  $('#endDate').datetimepicker({ 
		  minView : 2, 
		  autoclose : true,
		  format : 'yyyy-mm-dd' 
	});
	 
}
// 消息类型下拉列表
function initMsgTypeOpt(){
	$.getJSON("/rest/riskwarning/getAllAlarmType", function(result) {
		msgTypesObj=result;
		var option;
		for(var type in result){
			option+="<option value='"+result[type]+"'>"+type+"</option>";
		}
		$("#msgType").html(option);
	});
}
function beginSearch(){
	$("#infoDiv").html("");
	$("#detailDiv").hide();
	$("#infoDiv").show();
	var msgType=$("#msgType").val()==""?null:$("#msgType").val();
	var stationName=$("#stationName").val()==""?null:$("#stationName").val();
	var startDate=$("#startDate").val();
	var endDate=$("#endDate").val();
	if(startDate==""||endDate==""){
		
	}else{
		$.getJSON("/rest/riskwarning/getAlarmMessageByConditions/"+msgType+"/"+startDate+"/"+endDate+"/"+stationName, function(result) {
			if($.isEmptyObject(result)){
				//如果没有数据
				$("#noResult").show();
			}else{
				$("#noResult").hide();
				genMsgTbData(result);
			}
		});
	}
}
// 展示消息列表
var msgObj={};//表格需要的消息对象
function genMsgTbData(dataMap){
	for(var name in dataMap){
		var tbData=[];
		var box="<div class='box box-danger'><div class='box-header with-border'>"+name
		         //+"<div class='box-tools pull-right'><button class='btn btn-box-tool' data-widget='collapse'><i class='fa fa-minus'></i></button></div>" 
		         +"</div><div class='box-body'><table id='"+msgTypesObj[name]+"' class='table table-bordered table-hover dataTable'></table></div>"
				 +"</div>";
		$(box).appendTo($("#infoDiv"));
		for(var i in dataMap[name]){
			var data=dataMap[name][i];
			tbData.push([getTimeStr(data.createTime),data.content,data.source,data.msgType,data.id]);
		}
		msgObj[msgTypesObj[name]]=tbData;
	}
	genMsgTb();
}
var tb=null;
function genMsgTb(){
	for(var type in msgObj){
		var opt=MyTable.getDefaultOptions();
		opt.paging=true;
		opt.searching=true;
		opt.lengthChange=true;
		opt.columnDefs = [ {
			targets : [ 2, 3, 4],
			visible : false
		}];
		tb = new MyTable(type,['告警时间','告警内容',"source","msgType","id"],opt);
		tb.setDataSet(msgObj[type]);
		tb.onRowClick(function(data){
			var msgType=data[3];
			var sourceId=data[2];
			var id=data[4];
			openDetailPage(msgType,sourceId,id);
		});
		tb.show();
	}
}
//打开详情页面
function openDetailPage(msgType,sourceId,id){
	var pageUrl;
	switch (msgType) {
	case "ctbh":
		pageUrl="/ui/map/dialog/riskAlarmDialog/ctsaReportDlg.xhtml?oId="+ sourceId+"&msgId="+id;
		break;
	case "dlqyc":
	case "gzdlgldyc":
	case "xhsjyc":
		pageUrl="/ui/map/dialog/riskAlarmDialog/breakerActException.xhtml?recordId="
						+ sourceId + "&msgType=" + msgType+"&msgId="+id;
		break;
	case "hlyc":
		pageUrl="/ui/map/dialog/riskAlarmDialog/protectDeviceException.xhtml?oId="
						+ sourceId+"&msgId="+id;
		break;
	case "bhxyyc":
		$.getJSON("/rest/riskwarning/getAffectedObjectByTypeAndOid/" + id
				+ "/bhxyyc/" + sourceId, function(result) {
			if (result != null) {
				pageUrl="/ui/map/protectActSvg/protectActSvg.xhtml?lineId="
								+ result.objectId + "&faultTime="
								+ result.faultTime + "&lineName="
								+ result.objectName;
			}
		})
		break;
	case "tdyc_single":
	case "tdyc_double":
		pageUrl="/ui/map/dialog/riskAlarmDialog/protectChannelException.xhtml?oId="
						+ sourceId+"&msgId="+id;
		break;
	case "tzgj":
		pageUrl="/ui/map/dialog/riskAlarmDialog/tripException.xhtml?oId="
			+ sourceId+"&msgId="+id;
	}
	/*$.get(pageUrl,function(data){ 
		$("#infoDiv").html(data);
		$("#returnBtn").show();
	});*/
	//$("#returnBtn").show();
	$("#infoDiv").hide();
	$("#detailDiv").show();
	if(pageUrl){
		$("#detailPage").attr("src",pageUrl);
	}
}
function getTimeStr(time) {
	var t = new Date(time.time);
	return t.getFullYear() + "-" + doubleNum(t.getMonth() + 1) + "-"
			+ doubleNum(t.getDate()) + " " + doubleNum(t.getHours()) + ":"
			+ doubleNum(t.getMinutes()) + ":" + doubleNum(t.getSeconds());
}
function doubleNum(num) {
	return num > 9 ? num : '0' + num;
}
function hideReturnBtn(){
	//点击返回按钮后,保持展示原来的页面
	$("#detailDiv").hide();
	$("#infoDiv").show();
}
